package com.bw.flinkstreaming.transform.bw1;

import com.bw.flinkstreaming.source.bw1.NotParallelSource;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;

/**
 * 合并流
 * */
public class UnionTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //数据源1
        DataStreamSource<Long> source1 = env.addSource(new NotParallelSource());
        //数据源2
        DataStreamSource<Long> source2 = env.addSource(new NotParallelSource());

        //合并数据源1和数据源2，前提条件：两个数据源的类型必须是相同的
        DataStream<Long> source = source1.union(source2);
        SingleOutputStreamOperator<Long> mapResult = source.map(new MapFunction<Long, Long>() {
            @Override
            public Long map(Long value) {
                return value;
            }
        });

        SingleOutputStreamOperator<Long> sum = mapResult.timeWindowAll(Time.seconds(2)).sum(0);
        sum.print().setParallelism(1);
        env.execute("unointest");
    }
}
